import { NextResponse } from "next/server"

/**
 * 获取排行榜列表
 *
 * @route GET /api/rankings
 *
 * @returns {Object} 返回排行榜列表
 *
 * 请求示例:
 * GET /api/rankings
 *
 * 返回示例:
 * {
 *   "success": true,
 *   "data": [
 *     {
 *       "id": "rank001",
 *       "name": "热歌榜",
 *       "description": "最热门的歌曲排行",
 *       "updateFrequency": "每日更新"
 *     },
 *     {
 *       "id": "rank002",
 *       "name": "新歌榜",
 *       "description": "最新发布的歌曲排行",
 *       "updateFrequency": "每周更新"
 *     },
 *     // ... 更多排行榜
 *   ]
 * }
 */
export async function GET(request: Request) {
  try {
    // 实际项目中，这里应该从数据库或外部API获取数据
    const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || ""
    const response = await fetch(`${API_BASE_URL}/api/rankings`, {
      headers: {
        "Content-Type": "application/json",
      },
    })

    if (!response.ok) {
      throw new Error(`API request failed: ${response.statusText}`)
    }

    const data = await response.json()
    return NextResponse.json(data)
  } catch (error) {
    console.error("Error fetching rankings:", error)

    // 返回模拟数据作为备份
    return NextResponse.json({
      success: true,
      data: [
        {
          id: "rank001",
          name: "热歌榜",
          description: "最热门的歌曲排行",
          updateFrequency: "每日更新",
        },
        {
          id: "rank002",
          name: "新歌榜",
          description: "最新发布的歌曲排行",
          updateFrequency: "每周更新",
        },
        {
          id: "rank003",
          name: "飙升榜",
          description: "上升最快的歌曲排行",
          updateFrequency: "每日更新",
        },
        {
          id: "rank004",
          name: "流行指数榜",
          description: "最流行的歌曲指数排行",
          updateFrequency: "实时更新",
        },
        {
          id: "rank005",
          name: "原创榜",
          description: "优质原创音乐排行",
          updateFrequency: "每周更新",
        },
      ],
    })
  }
}

/**
 * 获取特定排行榜详情
 *
 * @route GET /api/rankings/[id]
 * @param {string} id - 排行榜ID
 *
 * @returns {Object} 返回特定排行榜详情和歌曲列表
 *
 * 请求示例:
 * GET /api/rankings/rank001
 *
 * 返回示例:
 * {
 *   "success": true,
 *   "data": {
 *     "id": "rank001",
 *     "name": "热歌榜",
 *     "description": "最热门的歌曲排行",
 *     "updateFrequency": "每日更新",
 *     "songs": [
 *       {
 *         "id": "song001",
 *         "name": "晴天",
 *         "artist": "周杰伦",
 *         "album": "叶惠美",
 *         "duration": "04:30",
 *         "rank": 1,
 *         "lastRank": 2
 *       },
 *       // ... 更多歌曲
 *     ]
 *   }
 * }
 */
export async function GETDetail(request: Request, { params }: { params: { id: string } }) {
  const id = params.id

  try {
    // 实际项目中，这里应该从数据库或外部API获取数据
    const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || ""
    const response = await fetch(`${API_BASE_URL}/api/rankings/${id}`, {
      headers: {
        "Content-Type": "application/json",
      },
    })

    if (!response.ok) {
      throw new Error(`API request failed: ${response.statusText}`)
    }

    const data = await response.json()
    return NextResponse.json(data)
  } catch (error) {
    console.error(`Error fetching ranking detail for id ${id}:`, error)

    // 返回模拟数据作为备份
    return NextResponse.json({
      success: true,
      data: {
        id: id,
        name:
            id === "rank001"
                ? "热歌榜"
                : id === "rank002"
                    ? "新歌榜"
                    : id === "rank003"
                        ? "飙升榜"
                        : id === "rank004"
                            ? "流行指数榜"
                            : "原创榜",
        description: "热门歌曲排行榜",
        updateFrequency: "每日更新",
        songs: [
          {
            id: "song001",
            name: "晴天",
            artist: "周杰伦",
            album: "叶惠美",
            duration: "04:30",
            rank: 1,
            lastRank: 2,
          },
          {
            id: "song002",
            name: "爱你",
            artist: "王心凌",
            album: "爱你",
            duration: "03:45",
            rank: 2,
            lastRank: 1,
          },
          {
            id: "song003",
            name: "青花瓷",
            artist: "周杰伦",
            album: "我很忙",
            duration: "03:59",
            rank: 3,
            lastRank: 5,
          },
          {
            id: "song004",
            name: "稻香",
            artist: "周杰伦",
            album: "魔杰座",
            duration: "03:42",
            rank: 4,
            lastRank: 3,
          },
          {
            id: "song005",
            name: "可惜没如果",
            artist: "林俊杰",
            album: "新地球",
            duration: "04:21",
            rank: 5,
            lastRank: 4,
          },
          // 更多歌曲...
        ],
      },
    })
  }
}

